/**
 * 		SearchAttributes
 * 			
 * This file is used for searching attribute information
 * 
 *	History: 	
 *   DATE 					AUTHOR 					DESCRIPTION
 * ----------------------------------------------------------------------------------------------------------
 * 24 - 01 - 2013			NguyenNV					Create new
 * 03 - 05 - 2013			NguyenNV					Update search attribute
 */
var ARR_SEARCH_VALUE;								   /* Array store values that is searching 	    	*/
$('#bt_search_ok').click(function(){
	SearchingAttribute.reset();
	$("#divMapLoading").show();
	if(onClickSearchAttr(ARR_SEARCH_VALUE,COMBO_ID_LAYER) != 0)
	{
		$("#content_list_bongden").hide();
		$("#table-content tr").remove(); 
		$(".south").show();
		$("#footer-page").show();
	}
	else{
		var south = $(".south");
		south.hide();
		$("#footer-page").hide();
	}
	$("#divMapLoading").hide();
});
$('#bt_search_cancel').click(function(){
	var south = $(".south");
	south.hide();
	var search = $(".search_attribute");
	search.hide();
	$("#footer-page").hide();
	$(".group-bt").hide();
	SearchingAttribute.deleteFileExport();
});

$(".bt-close").click(function(){
	var south = $(".south");
	south.hide();
	$("#footer-page").hide();
	SearchingAttribute.deleteFileExport();
	MapAction.RefreshAction();
});

$(".bt_tim_cancel_district").click(function(){
	$("#div_content_search_inside_district").hide();
});

function onClickDetailInfor(element){
	var featureId = element.attribute;
	FEATURE_ID = element.attribute;
	Graphic.HideDialogViewDetailInfo();
	Graphic.ShowDialogDetailInfo();
	setFeatureIdToArray(featureId);
	getFeatureDetailInfor(featureId);
	waitingDataAndDraw();
	var splId = featureId.split(".");
	if(splId[0].trim()==LayerAssetName.trambom){
		MayBom.searchAttr(featureId);
	}
}

function setFeatureIdToArray(_fid){
	
	ARR_FEATURE_DETAIL_INFOR = new Array();
	var splitData = _fid.split('.');
	var _layerName = splitData[0];
	
	var layer = CommonGeo.getLayerByID(_fid);
	
	for(var i=0; i < CurrentLayer.arrAlias.length;i++ )
	{

	if(CurrentLayer.arrAlias[i].LayerName == _layerName)
		{
			value = CommonGeo.getFeatureById(layer,_fid).attributes[CurrentLayer.arrAlias[i].Name];
			if(value == null) value = "";
			ARR_FEATURE_DETAIL_INFOR[CurrentLayer.arrAlias[i].Name] = value;
		}
	}
}
/*
 * 
 * Function: onClickSearchAttr	 											   							
 * Parameters:
 * 		 Parameter1: Void 															 																				
 * Return:	 
 * 		 void : 
 * 
 * Description:	Search attributes 
 * 
 * Author: NGUYENNV
 * Date : 2013/01/11										   												
 * 
 */

function onClickSearchAttr(_arrValue,idLayer)
{	
	$('#iddownloadexcel').hide();
	var feildKey = "";
	switch(idLayer)
	{
	case LayerAssetName.cheBienKhoangSan:
		feildKey = LayerFieldId.cheBienKhoangSan;
		break;
	case LayerAssetName.khaiThacKhoangSan:
		feildKey = LayerFieldId.khaiThacKhoangSan;
		break;
	case LayerAssetName.diemCapPhep:
		feildKey = LayerFieldId.diemCapPhep;
		break;
	case LayerAssetName.diemCapPhepCat:
		feildKey = LayerFieldId.diemCapPhepCat;
		break;
	case LayerAssetName.thamDoKS:
		feildKey = LayerFieldId.thamDoKS;
		break;
	case LayerAssetName.thamDoTemp:
		feildKey = LayerFieldId.thamDoTemp;
		break;
	case LayerAssetName.camKhaiThac:
		feildKey = LayerFieldId.camKhaiThac;
		break;
	case LayerAssetName.vungDauTu:
		feildKey = LayerFieldId.vungDauTu;
		break;
	case LayerAssetName.vungQuyHoach:
		feildKey = LayerFieldId.vungQuyHoach;
		break;
	case LayerAssetName.vungKsTv:
		feildKey = LayerFieldId.vungKsTv;
		break;
	case LayerAssetName.hoDieuHoa:
		feildKey = LayerFieldId.hoDieuHoa;
		break;
	case LayerAssetName.cauTrucChuyenDong:
		feildKey = LayerFieldId.cauTrucCT;
		break;
	case LayerAssetName.luuVuc:
		feildKey = LayerFieldId.luuVuc;
		break;
	case LayerAssetName.suCo:
		feildKey = LayerFieldId.suCo;
		break;
	case LayerAssetName.viPham:
		feildKey = LayerFieldId.viPham;
		break;
	case LayerAssetName.khuVucNgap:
		feildKey = LayerFieldId.ngapUng;
		break;
	case LayerAssetName.scada:
		feildKey = LayerFieldId.scada;
		break;
	case LayerAssetName.ongThoatNuoc:
		feildKey = LayerFieldId.ongThoatNuoc;
		break;
	}
	Graphic.clear();
	ARR_LAYER_PRIMARY = new Array();
	ARR_LAYER_RESULT = new Array();
	// START NGUYENNV ADD 140102
	SearchingAttribute.arrResult = new Array();
	// END NGUYENNV ADD 140102
	var array_result = new Array(); 
	var arrayAllFid = new Array();
	ARR_LAYER_PRIMARY = CommonGeo.getAttributeOfLayer(idLayer);
	var Layer = CommonGeo.getLayerByIdLayer(idLayer);
	var count = 0;
	var isFieldNotNull = false;
	Graphic.columId = feildKey;
	for(var j=0; j<ARR_LAYER_PRIMARY.length; j++)
		{
			arrayAllFid[arrayAllFid.length] = ARR_LAYER_PRIMARY[j].attribute["fid"];
		}
	for ( var i = 0; i < CurrentLayer.arrAlias.length; i++) {
			if( CurrentLayer.arrAlias[i].IsSearch == false) continue;
			if( CurrentLayer.arrAlias[i].Type=="img")continue;
			var field = CurrentLayer.arrAlias[i].Name;
			if(CurrentLayer.arrAlias[i].Type == "date" )
			{
				if(_arrValue[field+"from"] == "" && _arrValue[field+"to"] =="")
					_arrValue[field] = "";
			}
			// If field search is null
			if(_arrValue[field] == null || _arrValue[field] == -1 || _arrValue[field] == "") continue;
			// else if field have value
			else
				{
					// Start test NguyenNV 20121126
					ARR_LAYER_RESULT = new Array();
					isFieldNotNull = true;
					// In case of searching by date
					if(CurrentLayer.arrAlias[i].Type == "date" && _arrValue[field].length == 10)
						{
						   
							count = 0;
							array_result = new Array();
							SearchingAttribute.arrResult = new Array();
							
							for(var j=0; j<ARR_LAYER_PRIMARY.length; j++)
							{
								if(CommonFuns.checkDateBetweenRank(ARR_LAYER_PRIMARY[j].attribute[field]
													, _arrValue[field+"from"], _arrValue[field+"to"]) == true )
									{
										ARR_LAYER_RESULT[count] = ARR_LAYER_PRIMARY[j];
										array_result[array_result.length] = ARR_LAYER_PRIMARY[j].attribute["fid"];
										SearchingAttribute.arrResult[SearchingAttribute.arrResult.length] = ARR_LAYER_PRIMARY[j].attribute[feildKey];
										count++;
									}
							}
						}
					else{ // In other case (not by date)
							count = 0;
							array_result = new Array();
							SearchingAttribute.arrResult = new Array();
							
							for(var j=0; j<ARR_LAYER_PRIMARY.length; j++)
							{
								var str = _arrValue[field];
								var _str = ARR_LAYER_PRIMARY[j].attribute[field];
								var re = new RegExp(str,"gi");
								if(_str.match(re) != null)
								{
									array_result[array_result.length] = ARR_LAYER_PRIMARY[j].attribute["fid"];
									SearchingAttribute.arrResult[SearchingAttribute.arrResult.length] = ARR_LAYER_PRIMARY[j].attribute[feildKey];
									ARR_LAYER_RESULT[count] = ARR_LAYER_PRIMARY[j];
									count++;
								}
							}
						}
					ARR_LAYER_PRIMARY = new Array();
					ARR_LAYER_PRIMARY = ARR_LAYER_RESULT;
					// End test NguyenNV 20121126
				}
	}
		if(isFieldNotNull == false){
			Graphic.init();
			Graphic.isShowView = true;
			if(idLayer==LayerAssetName.phanhoiKH)
				Graphic.isShowAddInfo = true;
			else if(idLayer==LayerAssetName.khuVucNgap)
				Graphic.isShowAddInfo = true;
			Graphic.divPaging = base.getElementById('footer-page');
			Graphic.tableContent = "table-content";
			Graphic.displayList(arrayAllFid);
			Graphic.waitingData();
			Graphic.drawFooter();
			if(arrayAllFid.length>0){}
				//SearchingAttribute.exportExcel();
		}
		else{
			if(count == 0) {
					var south = $(".south");
					south.hide();
					alert(LANGUAGE["no_result"]);
					return 0;
				}
			else
				{
					Graphic.init();
					Graphic.isShowView = true;
					if(idLayer==LayerAssetName.phanhoiKH)
						Graphic.isShowAddInfo = true;
					else if(idLayer==LayerAssetName.khuVucNgap)
						Graphic.isShowAddInfo = true;
					Graphic.divPaging = base.getElementById('footer-page');
					Graphic.tableContent = "table-content";
					Graphic.displayList(array_result);
					Graphic.waitingData();
					Graphic.drawFooter();
					if(array_result.length > 0){
						//SearchingAttribute.exportExcel();
					}
				}
		}
}

var arrTD_Attributes = [];
SearchingAttribute = 
{
		layerId: '',
		divSlectLayer: "select_layer_search_attribute",
		// START NguyenNV add new 140102
		arrResult: [], // Store the searching result 
		// END NguyenNV add new 140102
		/*
		 * 
		 * Function: onClickQuit	 											   							
		 * Parameters:
		 * 		 Parameter1: Void 															 																				
		 * Return:	 
		 * 		 void : 
		 * 
		 * Description:	Onclick when clicking quit button
		 * 
		 * Author: NGUYENNV
		 * Date : 2013/01/11										   												
		 * 
		 */
		init:function(){
			var divId = base.getElementById('footer-page');
			base.removeChildDiv(divId);
			var prev = document.createElement("input");
			prev.type = 'button';
			prev.className = 'paging-number paging-prev';
			//prev.value = LANGUAGE["back"];
			prev.value = "";
			$(".paging").append(prev);
			var next = document.createElement("input");
			next.type = 'button';
			next.className = 'paging-number paging-next';
			//next.value = LANGUAGE["next"];
			next.value = "";
			$(".paging").append(next);
		},
		reset:function(){
			var listbongden = base.getElementById('content_list_bongden');
			var footer = base.getElementById('footer-page');
			if(listbongden != null)
				base.removeChildDiv(listbongden);
			if(footer != null)
				base.removeChildDiv(footer);
			$("#table-content tr").remove(); 
			$(".south").hide();
		},
		onClickQuit:function(){
			 // remove
			base.removeChildDiv(base.getElementById(DIV_SEARCH_CONTENT));
			base.removeChildDiv(base.getElementById(DIV_DANH_SACH_TAI_SAN));
			base.getElementById(DIV_MAP_CONTENT).style.display = "none";
			base.removeChildDiv(base.getElementById("idMapContentHeader"));
		},
		close:function(){
			var south = $(".south");
			south.hide();
			var search = $(".search_attribute");
			search.hide();
		},
		onChangePhuongXa:function(){
			var data = this.value;
			ARR_SEARCH_VALUE[this.attribute] = data; 
			$("#select_searching_attributematuyenduong option").each(function(){
					var title = $(this).attr('title');
					if(title!="undefined"&&title!=data){
						$(this).hide();
					}else if(title!="undefined"&&title==data){
						$(this).show();
					}
			});
			
		},
		/*															 															
		 * Description:	event onClick zoom to quan huyen 
		 * Author: NGUYENNV
		 * Date : 2013/07/01										   												
		 */
		onClickZoomToDistrict:function(element){
			CommonGeo.zoomToDistrict(element.id);
		},
		/*															 															
		 * Description:	event onClick zoom to phuong xa 
		 * Author: NGUYENNV
		 * Date : 2013/07/01										   												
		 */
		onClickZoomToCommun:function(element){
			CommonGeo.zoomToCommun(element.id);
		},
		/*															 															
		 * Description:	event onClick zoom to tuyen duong 
		 * Author: NGUYENNV
		 * Date : 2013/07/01										   												
		 */
		onClickZoomToTuyenDuong:function(){
			
		},
		onkeypressTimKiem:function(element,event){
			var evt = event || window.event;
			if(evt.keyCode == 13)
			{
				timKiemChung();
			}
		},
		/*
		 * 
		 * Function: onChangeQuanHuyen	 											   							
		 * Parameters:
		 * 		 Parameter1: Void 															 																				
		 * Return:	 
		 * 		 void : 
		 * 
		 * Description:	onChange when selecting District 
		 * 
		 * Author: NGUYENNV
		 * Date : 2013/01/11										   												
		 * 
		 */
		onChangeQuanHuyen:function(){
			var idQuanHuyen = this.value;
			ARR_SEARCH_VALUE[this.attribute] = this.value;
			ARR_SEARCH_VALUE["maphuong"] = -1;
					var comboPhuongXa = base.getElementById("idComboPhuongXa");
					  if (comboPhuongXa.length > 0)
						  while(comboPhuongXa.length > 0)
							  {
							  	comboPhuongXa.remove(comboPhuongXa.length-1);
							  }
					  if(idQuanHuyen > 0)
						  Graphic.comboPhuongXa(comboPhuongXa,idQuanHuyen);
			$.uniform.update("#idComboPhuongXa");
		},
		/*
		 * 
		 * Function: zoomAndDraw	 											   							
		 * Parameters:
		 * 		 Parameter1: object
		 *  	 Parameter2: array														 																				
		 * Return:	 
		 * 		 void : 
		 * 
		 * Description:	Zoom and marked features 
		 * 
		 * Author: NGUYENNV
		 * Date : 2013/01/11										   												
		 * 
		 */
		zoomAndDraw:function(_layer, _array){
			
			var _bounds=null;
			
			for(var i=0; i <_array.length; i++ )
				{	
				var feature = CommonGeo.getFeatureById(_layer,_array[i]);
				if(feature != null)
					{
					_layer.drawFeature(feature, "select");
					if (_bounds == null)
						_bounds = feature.geometry.getBounds().clone();
						_bounds.extend(feature.geometry.getBounds());
					}
				}
			if (_bounds != null)
				map.zoomToExtent(_bounds, false);
		},
		/*
		 * 
		 * Function: onChangeSearchFromDate	 											   							
		 * Parameters:
		 * 		 Parameter1: Void 															 																				
		 * Return:	 
		 * 		 void : 
		 * 
		 * Description:	onChange when selecting date 
		 * 
		 * Author: NGUYENNV
		 * Date : 2013/01/11										   												
		 * 
		 */
		onChangeSearchFromDate:function(){
			var date = this.value;
			if(date.length == 10){
				ARR_SEARCH_VALUE[this.attribute] = date;
				var splitDate = date.split('/');
				var year = parseInt(splitDate[2]);
				var month = parseInt(splitDate[1]);
				var day = parseInt(splitDate[0]); 
				if(month<10) month = "0" + month;
				if(day<10) day = "0" + day;
				var date = year + "-" + month + "-" + day;
				ARR_SEARCH_VALUE[this.attribute+"from"] = date;
			}else
					ARR_SEARCH_VALUE[this.attribute+"from"] = "";
		},
		/*
		 * 
		 * Function: onChangeSearchToDate	 											   							
		 * Parameters:
		 * 		 Parameter1: Void 															 																				
		 * Return:	 
		 * 		 void : 
		 * 
		 * Description:	onChange when selecting date 
		 * 
		 * Author: NGUYENNV
		 * Date : 2013/01/11										   												
		 * 
		 */
		onChangeSearchToDate:function(){
			
			var date = this.value;
			if(date.length == 10){
				ARR_SEARCH_VALUE[this.attribute] = date;
				var splitDate = date.split('/');
				var year = parseInt(splitDate[2]);
				var month = parseInt(splitDate[1]);
				var day = parseInt(splitDate[0]); 
				if(month<10) month = "0" + month;
				if(day<10) day = "0" + day;
				var date = year + "-" + month + "-" + day;
				ARR_SEARCH_VALUE[this.attribute+"to"] = date;
			}
			else
				ARR_SEARCH_VALUE[this.attribute+"to"] = "";
				
		},
		onChangeText:function(){
			ARR_SEARCH_VALUE[this.attribute] = this.value;
		},
		onChangeCombox:function(){
			ARR_SEARCH_VALUE[this.attribute] = this.value;
		},
		/*
		 * 
		 * Function: DrawContent	 											   							
		 * Parameters:
		 * 		 Parameter1: string
		 *  	 Parameter2: string														 																				
		 * Return:	 
		 * 		 void : 
		 * 
		 * Description:	Draw content of searching form 
		 * 
		 * Author: NGUYENNV
		 * Date : 2013/01/11										   												
		 * 
		 */
		drawContent:function(DivContent ,_idLayer){
			arrTD_Attributes = [];
			base.removeChildDiv(DivContent);
			var fieldSet = document.createElement("fieldset");
			fieldSet.setAttribute("class", "form-mapleft-menu");
			var sID = "form_ALIAS_mapleft_menu_select";
			fieldSet.id = sID;
			var TableContent = document.createElement("table");
			fieldSet.appendChild(TableContent);
			TableContent.border="5";
				for(var i=0; i< CurrentLayer.arrAlias.length; i++ )
					{	
						if(CurrentLayer.arrAlias[i].IsSearch && CurrentLayer.arrAlias[i].Name != "anhminhhoa" )
							{
								var comment = "";
								if(CurrentLayer.arrAlias[i].IsComment){
									var text = CurrentLayer.arrAlias[i].DataType;
									if(text=="m3")text="m&sup3;/h";
									else if(text=="m2")text="m&sup2;";
									else if(text=="m3k")text="m&sup3;";
									comment = " ("+text+")";
								}
								var TR_Object = document.createElement("tr");
								var TD_ClmA = document.createElement("td");
								var TD_ClmB = document.createElement("td");
								TD_ClmA.style.width = "200px";
								TD_ClmA.style.padding = CELL_PADDING;
								var alias = CurrentLayer.arrAlias[i].Alias+comment;
								TD_ClmA.innerHTML  = alias;
								TR_Object.appendChild(TD_ClmA);
								TR_Object.appendChild(TD_ClmB);
								TableContent.appendChild(TR_Object);
								if(CurrentLayer.arrAlias[i].Name == "maquan")
									{
										/*var selectContrlQH = document.createElement("select");
										var divInput = document.createElement("div");
										divInput.setAttribute("class", "input input-mdf wd-s1");
										divInput.appendChild(selectContrlQH);
										selectContrlQH.style.width = WIDTH_SELECT_CONTRL + "px";
										selectContrlQH.attribute = CurrentLayer.arrAlias[i].Name;
										selectContrlQH.onchange = SearchingAttribute.onChangeQuanHuyen;
										Graphic.comboQuanHuyen(selectContrlQH);
										TD_ClmB.appendChild(divInput);*/
										continue;
									}
								if(CurrentLayer.arrAlias[i].Name == "maphuong")
									{
										var selectContrlPX = document.createElement("select");
										var divInput = document.createElement("div");
										divInput.setAttribute("class", "input input-mdf wd-s1");
										divInput.appendChild(selectContrlPX);
										selectContrlPX.id = "idComboPhuongXa";
										selectContrlPX.attribute = CurrentLayer.arrAlias[i].Name;
										selectContrlPX.onchange = SearchingAttribute.onChangePhuongXa;
										selectContrlPX.style.width = WIDTH_SELECT_CONTRL + "px";
										Graphic.comboPhuongXa(selectContrlPX, "540");
										TD_ClmB.appendChild(divInput);
										$(selectContrlPX).uniform();
										continue;
									}
							
								if (CurrentLayer.arrAlias[i].IsCategory) {
									arrTD_Attributes[CurrentLayer.arrAlias[i].Name] = TD_ClmB;
									var select_Text_Alias = document.createElement("select");
									select_Text_Alias.id = "select_searching_attribute"+CurrentLayer.arrAlias[i].Name;
		 							select_Text_Alias.style.width = WIDTH_SELECT_CONTRL + "px";
		 							var option_Text_Alias = document.createElement("option");
		 							option_Text_Alias.selected = "selected";
		 							option_Text_Alias.value = -1;
		 							$(option_Text_Alias).text(LANGUAGE["tat_ca"]);
		 							option_Text_Alias.id = "data_searching_attribute"+CurrentLayer.arrAlias[i].Name;
		 							select_Text_Alias.appendChild(option_Text_Alias);
		 							select_Text_Alias.onchange = SearchingAttribute.onChangeCombox;
									$.ajax({
										type:"POST",
										url:sPortletJSP+ "GetData.jsp",
										data:{
											attr:CurrentLayer.arrAlias[i].FieldValue,
											table:CurrentLayer.arrAlias[i].TableName,
											asset:COMBO_ID_LAYER
										},
										async:false,
										success:function(data){
											var result = data.trim();
											var option = "<option value='-1' selected='selected' id='option_none_"+CurrentLayer.arrAlias[i].Name+"'>"+LANGUAGE["tat_ca"]+"</option>";
											option = option+result;
											$(select_Text_Alias).html(option);
											
											var divInput = document.createElement("div");
											divInput.setAttribute("class", "input input-mdf wd-s1");
											divInput.appendChild(select_Text_Alias);
				 							arrTD_Attributes[CurrentLayer.arrAlias[i].Name].appendChild(divInput);
											$(select_Text_Alias).each(function() {
												var selectedValue = $(this).val();
												$(this).html($("option", $(this)).sort(function(a, b) {
													 if(b.value!="0")return a.text.toLowerCase() == b.text.toLowerCase() ? 0 : a.text.toLowerCase() < b.text.toLowerCase() ? -1 : 1;
												}));
												$(this).val(selectedValue);
											});
											$(select_Text_Alias).uniform();
										}
									});
					 				/*var $divMap_Data = $(document.createElement('div'));
					 				$divMap_Data.attr("Attr_Name", CurrentLayer.arrAlias[i].Name);
					 				arrTD_Attributes[CurrentLayer.arrAlias[i].Name] = TD_ClmB;
					 				var sFileJSP = "GetData.jsp";
					 				$divMap_Data.load(sPortletJSP + sFileJSP 
					 						+ "?attr=" + CurrentLayer.arrAlias[i].FieldValue
					 						+"&asset="+CurrentLayer.layerId
					 						+"&table="+ CurrentLayer.arrAlias[i].TableName, function(data) {
					 							var select_Text_Alias = document.createElement("select");
					 							select_Text_Alias.style.width = WIDTH_SELECT_CONTRL + "px";
					 							var option_Text_Alias = document.createElement("option");
					 							option_Text_Alias.selected = "selected";
					 							option_Text_Alias.value = -1;
					 							$(option_Text_Alias).text(LANGUAGE["tat_ca"]);
					 							option_Text_Alias.id = "date_searching_attribute"+ $(this).attr("Attr_Name");
					 							select_Text_Alias.appendChild(option_Text_Alias);
					 							var splitData = data.split('<p>');
					 							for ( var iItem = 0; iItem < splitData.length; iItem++) {
													var splitData_Item = splitData[iItem].split('<br>');
													if (splitData_Item.length < 2) continue;
													var optionItem = document.createElement('option');
													optionItem.id = "searching_attribute" + $(this).attr("Attr_Name") + "_" + iItem;
													$(optionItem).text(splitData_Item[1]);
													var objValueItem = splitData_Item[0];
													objValueItem = objValueItem.replace(/(\r\n|\n|\r)/gm, "");
													optionItem.value = objValueItem;
													select_Text_Alias.appendChild(optionItem);
												}
					 							select_Text_Alias.onchange = SearchingAttribute.onChangeCombox;
					 							var divInput = document.createElement("div");
												divInput.setAttribute("class", "input input-mdf wd-s1");
												divInput.appendChild(select_Text_Alias);
					 							arrTD_Attributes[$(this).attr("Attr_Name")].appendChild(divInput);
					 							select_Text_Alias.attribute = $(this).attr("Attr_Name");
					 							$(select_Text_Alias).each(function() {
													var selectedValue = $(this).val();
													$(this).html($("option", $(this)).sort(function(a, b) {
														return a.text.toLowerCase() == b.text.toLowerCase() ? 0 : a.text.toLowerCase() < b.text.toLowerCase() ? -1 : 1;
													}));
													$(this).val(selectedValue);
												});
												$(option_Text_Alias).css("display","none");
					 							$("#" + sID + " select").uniform();
					 				});*/
					 		 	}
								else if(CurrentLayer.arrAlias[i].Type == "date")
				 		 		{
				 		 			var objLabel_from = document.createElement("label");
						 			objLabel_from.innerHTML = LANGUAGE["from_date"];
						 			
						 			var objLabel_ToDate = document.createElement("label");
				 					objLabel_ToDate.innerHTML = LANGUAGE["to_date"];
				 					
				 					ARR_SEARCH_VALUE[CurrentLayer.arrAlias[i].Name+"from"] = "";
				 					ARR_SEARCH_VALUE[CurrentLayer.arrAlias[i].Name+"to"] = "";
				 					ARR_SEARCH_VALUE[CurrentLayer.arrAlias[i].Name] = "";
				 		 		 	
				 		 			var input_Text_Alias = document.createElement("input");
				 		 			var divInput = document.createElement("div");
									divInput.setAttribute("class", "input input-mdf wd-s1");
									divInput.appendChild(input_Text_Alias);
									
				 		 			input_Text_Alias.type = "text";
				 		 			input_Text_Alias.onchange = SearchingAttribute.onChangeSearchFromDate;
				 		 			input_Text_Alias.id = sNameSpace + CurrentLayer.arrAlias[i].Name;
				 		 
				 		 			$date = $(input_Text_Alias);
				 		 			$date
				 		 				.datepicker({
										numberOfMonths : 1,
										constrainInput : true,
										dateFormat : 'dd/mm/yy',
										showButtonPanel : false,
										hideIfNoPrevNext : true,
										autoSize : false,
										onSelect : function(date) {
											var splitDate = date.split('/');
											var year = parseInt(splitDate[2]);
											var month = parseInt(splitDate[1]);
											var day = parseInt(splitDate[0]); 
											if(month<10) month = "0" + month;
											if(day<10) day = "0" + day;
											var date = year + "-" + month + "-" + day;
											
											ARR_SEARCH_VALUE[this.attribute + "from"] = date;
											ARR_SEARCH_VALUE[this.attribute] = date;
										}
									});
				 		 			var currentTime = new Date();
				 		 			var month = currentTime.getMonth() + 1;
				 		 			var day = currentTime.getDate();
				 		 			if(month<10) month = "0" + month;
				 		 			if(day<10) day = "0" + day;
				 		 			input_Text_Alias.attribute = CurrentLayer.arrAlias[i].Name;
				 		 			TD_ClmB.appendChild(divInput);
				 					TD_ClmA.appendChild(objLabel_from);
				 					
				 					
				 					var TR_ToDate = document.createElement("tr");
				 					var TD_A_ToDate = document.createElement("td");
				 					var TD_B_ToDate = document.createElement("td");
				 					TD_A_ToDate.style.padding = CELL_PADDING;
				 					//TD_B_ToDate.style.padding = CELL_PADDING;
				 					TD_A_ToDate.innerHTML  = CurrentLayer.arrAlias[i].Alias ;
				 					
				 					var input_Text_toDate = document.createElement("input");
				 					input_Text_toDate.type = "text";
				 					input_Text_toDate.onchange = SearchingAttribute.onChangeSearchToDate;
				 					input_Text_toDate.attribute = CurrentLayer.arrAlias[i].Name;
				 					input_Text_toDate.id = sNameSpace + CurrentLayer.arrAlias[i].Name +"to";
				 					
				 		 			$date = $(input_Text_toDate);
				 		 			$date.datepicker({
										numberOfMonths : 1,
										constrainInput : true,
										dateFormat : 'dd/mm/yy',
										showButtonPanel : false,
										hideIfNoPrevNext : true,
										autoSize : false,
										onSelect : function(date) {
											
											var splitDate = date.split('/');
											var year = parseInt(splitDate[2]);
											var month = parseInt(splitDate[1]);
											var day = parseInt(splitDate[0]); 
											if(month<10) month = "0" + month;
											if(day<10) day = "0" + day;
											var date = year + "-" + month + "-" + day;
											
											ARR_SEARCH_VALUE[this.attribute+"to"] = date;
											ARR_SEARCH_VALUE[this.attribute] = date;
										}
									});
				 		 			
				 		 			var divInput = document.createElement("div");
									divInput.setAttribute("class", "input input-mdf wd-s1");
									divInput.appendChild(input_Text_toDate);
									
				 		 			TD_B_ToDate.appendChild(divInput);
				 		 			TD_A_ToDate.appendChild(objLabel_ToDate);
				 					TR_ToDate.appendChild(TD_A_ToDate);
				 					TR_ToDate.appendChild(TD_B_ToDate);
				 					TableContent.appendChild(TR_ToDate);
				 		 		}
								else
				 				{
				 		 			var input_Text_Alias = document.createElement("input");
				 		 			input_Text_Alias.type = "text";
				 		 			input_Text_Alias.name = CurrentLayer.arrAlias[i].Name;
				 		 			input_Text_Alias.attribute = CurrentLayer.arrAlias[i].Name;
				 		 			input_Text_Alias.onchange = SearchingAttribute.onChangeText;
				 		 			input_Text_Alias.id = sNameSpace + "text_searching_attribute" + CurrentLayer.arrAlias[i].Name;
				 		 			if (CurrentLayer.arrAlias[i].Type == "int") {
				 						input_Text_Alias.IsDecimal = 0;
				 						input_Text_Alias.onkeydown = txtKeyDown;
				 					}
				 					if (CurrentLayer.arrAlias[i].Type == "decimal") {
				 						input_Text_Alias.IsDecimal = 1;
				 						input_Text_Alias.onkeydown = txtKeyDown;
				 					}
				 					var divInput = document.createElement("div");
									divInput.setAttribute("class", "input input-mdf wd-s1");
									divInput.appendChild(input_Text_Alias);
									
				 		 			TD_ClmB.appendChild(divInput);
				 				}
							}
					}
			  DivContent.appendChild(fieldSet);
		},
		/*
		 * 
		 * Function: DrawSearchBody	 											   							
		 * Parameters:
		 * 		 Parameter1: string 															 																				
		 * Return:	 
		 * 		 void : 
		 * 
		 * Description:	Draw content on popup 
		 * 
		 * Author: NGUYENNV
		 * Date : 2013/01/11										   												
		 * 
		 */
		drawSearchBody:function(_idLayer){
	 			var ParentDiv = base.getElementById(DIV_SEARCH_CONTENT);
	 			ParentDiv.style.display = "block";
		 		SearchingAttribute.drawContent(ParentDiv,_idLayer);
		},
		/*
		 * 
		 * Function: onChangeSelectLayer	 											   							
		 * Parameters:
		 * 		 Parameter1: Void 															 																				
		 * Return:	 
		 * 		 void : 
		 * 
		 * Description:	onChange when selecting layer 
		 * 
		 * Author: NGUYENNV
		 * Date : 2013/01/11										   												
		 * 
		 */
		onChangeSelectLayer: function(ctrl){
			var index = ctrl.value;
			MapAction.DeActive();
			var search = $(".search_attribute");
			if(index <= 0)
				{
					search.hide();
					return 0;
				}
			LayerId_Asset_Search = ctrl.title;
			MapAction.HideWFSAll();
			map.layers[index].setVisibility(true);
			CurrentFunction.close();
			search.show();
			var layer =  vectorArray[index-numBaseLayer];
			COMBO_ID_LAYER = layer.LayerId;
			this.layerId = COMBO_ID_LAYER;
			CurrentLayer.create();
			CurrentLayer.layerId = COMBO_ID_LAYER;
			CurrentFunction.name = Fuctions.searchAttributes;
			ARR_SEARCH_VALUE = new Array();
			CurrentLayer.arrAlias = CommonGeo.setCurrentAlias(COMBO_ID_LAYER);
			if(CurrentLayer.arrAlias.length > 0)
			{
				SearchingAttribute.drawSearchBody(COMBO_ID_LAYER);
				$("#content_list_bongden").empty();
				$("#pageNavPosition").empty();
				$(".group-bt").show();
			}
		},
		exportExcel:function(){
			$('#iddownloadexcel').hide();
			var sFileJSP = "ExportExcel.jsp";
			// Get excel file name
			var fileName = SearchingAttribute.createFileName()+".xls";
			var realPath =  $('#txtRealPath').val()+fileName;
			$('#iddownloadexcel').attr("href", realPath);
			SearchingAttribute.arrResult = JSON.stringify(SearchingAttribute.arrResult);
			$.ajax({
				url: sPortletJSP + sFileJSP,
			    type: "POST",
			    data: {
			        myArray : SearchingAttribute.arrResult,
			        layerId: COMBO_ID_LAYER,
			        filename: fileName
			    },
			    async:false,
		        success: function(data) {
		        	var result = data.trim();
		        	if(result == "0")
		        		$('#iddownloadexcel').hide();
		        	else {
		        		$('#iddownloadexcel').show();
		        	}
		        }
			});
		},
		createFileName:function(){
			var time;
			var d = new Date();
			time = d.getTime();
			return time;
		},
		deleteFileExport:function(){
			var urlFile = $("#iddownloadexcel").attr("href");
			var splFile = urlFile.split("/");
			var name = splFile[splFile.length-1];
			$.ajax({
			 	type:"POST",
				url:sPortletJSP+"GetData.jsp",
				data:{
					attr:"delete_file_export",
					file:name
				},
				async:false,
				success:function(data){
				} 
	 		});
		}
};
